<?php
/**
 * @title 39. 组合总和 - 递归简化 + 剪枝
 * @author start2004
 */

ini_set("memory_limit", "128M");

class Solution {

    /**
     * @param Integer[] $candidates
     * @param Integer $target
     * @return Integer[][]
     */
    function combinationSum($candidates, $target) {
        /**
         * @since 2020-08-13 排序, 便于剪枝
         */
        sort($candidates);

        /**
         * @return
         */
        return self::find($candidates, 0, count($candidates), $target);
    }

    /**
     * @title 匹配总和
     * @author start2004
     * @since 2020-08-13 Created
     *
     * @param array $candidates
     * @param int $key
     * @param int $len
     * @param int $target
     * @return array
     */
    public function find($candidates = [], $key = 0, $len = 0, $target = 0){
        /**
         * @since 2020-08-13 到达边界
         */
        if($key == $len){
            return [];
        } else {}

        /**
         * @since 2020-08-13 结果集
         */
        $result = [];

        /**
         * @since 2020-08-13 数组升序, target不大于等于$candidates[$key], 无解, 提前剪枝
         */
        if($target == $candidates[$key]){
            $result[] = [$candidates[$key]];
        } elseif($target > $candidates[$key]) {
            /**
             * @since 2020-08-13 $candidates[$key], 数量从0开始, 累计总和不能超过target
             */
            $sum = 0;
            $nums = [];
            while($sum <= $target){
                /**
                 * @since 2020-08-13 匹配成功, 无需后续节点
                 */
                if($sum == $target){
                    $result[] = $nums;
                } else {
                    /**
                     * @since 2020-08-13 后续节点补充target-sum
                     */
                    $nextResult = self::find($candidates, $key+1, $len, $target-$sum);

                    /**
                     * @since 2020-08-13 存在可能性
                     */
                    foreach ($nextResult as $next){
                        $result[] = array_merge($nums, $next);
                    }
                }

                /**
                 * @since 2020-08-13 $candidates[$key]数量+1
                 */
                $nums[] = $candidates[$key];
                $sum += $candidates[$key];
            }
        } else {}

        /**
         * @return
         */
        return $result;
    }
}




/**
 * @url http://127.0.0.1/leetcode/202007/2020.07.15_6.php
 */
$datas = [
    [[2,4], 3],
    // [[2,3,6,7],7,],
    [[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,],28],
    // [[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,],200],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof(2);
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->combinationSum($data[0], $data[1]);
    if(count($result)<20){
        var_dump($result);
    } else {
        var_dump(count($result));
    }
    echo str_repeat("<br>", 3);
}
$xhprof->end();
